/**
 * zmn渠道管理-佣金设置
 * 文件命名格式：zmn.模块.表/功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:06
 */
layui.define(['form', 'layedit', 'laydate', 'table', 'laytpl'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    let table = layui.table;
    var laytpl = layui.laytpl;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        limit: 10000,
        height: 'full-140'
    });

    form.render(null, "zmn-form");
    renderByAccessIdentity(json.obj.accessIdentity);

    function extracted(data) {
        laytpl(modifyHtml.innerHTML).render(data, function (html) {
            childIndex = layer.open({
                type: 1,
                title: '修改佣金比例'
                , content: html
                , area: ['600px', '330px']
            });

            laydate.render({
                elem: '#effectiveTime'
                , type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss'
            });
            change(data.commissionTypeNew);
            form.render(null, "zmn-form");
        });
    }

//【表格右侧工具栏监听】
    table.on('tool(zmn-table)', function (obj) {
        if (obj.event === 'modify') {
            extracted(obj.data);
        }
    });

    // 自定义验证规则
    form.verify({
        range_0_12: function(value){
            if(value > 12){ return "比例范围 0~12%"; };
            if(value < 0){ return "比例范围 0~12%"; };
            var reg = new RegExp("^[0-9]+.?[0-9]{0,1}$");
            if(!reg.test(value)){ return "请输入正确的格式!#.#"; }
        },
        range_0_30: function(value){
            if(value > 30){ return "金额范围 0~30元"; };
            if(value < 0){ return "金额范围 0~30元"; };
            var reg = new RegExp("^[0-9]+.?[0-9]{0,1}$");
            if(!reg.test(value)){ return "请输入正确的格式!#.#"; }
        }
    });

    form.on('radio(commissionTypeNew)', function(obj){
        var type = obj.value;
        change(type);
    });

    form.on('radio(settle)', function (obj) {
        let data = new Object();

        if (obj.value == 1) {
            $('#deductDiv').hide();
            data.deduct = null;
        } else {
            $('#deductDiv').show();
            data.deduct = $('input[name="deduct"]:checked').val();
        }

        data.settle = obj.value;
        data.channelId = $('#channelId').val();
        data.relateId = $('#relateId').val();

        submitCommissionSettleRule(data);
    });

    form.on('radio(deduct)', function (obj) {
        let data = new Object();
        data.deduct = obj.value;
        data.settle = $('input[name="settle"]:checked').val();
        data.channelId = $('#channelId').val();
        data.relateId = $('#relateId').val();

        submitCommissionSettleRule(data);
    });

    function submitCommissionSettleRule(data) {
        var url = "/channel/commission/settle/rule/addEdit.acion";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json",
            dataType: "json", data: JSON.stringify(data),
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if(resp.status == 200) {
                    zmnPage.reload(resp.message, 1000);
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    }
    
    function renderByAccessIdentity(accessIdentity) {
        // 接入身份只要包含渠道选项 则佣金配置按钮可用  否则不可用
        if (accessIdentity.indexOf("1") != -1) {
            $('input[name="deduct"]').removeClass("layui-radio-disabled").removeClass("layui-disabled").attr('disabled', false);
            $('input[name="settle"]').removeClass("layui-radio-disabled").removeClass("layui-disabled").attr('disabled', false);
        } else {
            $('input[name="deduct"]').addClass("layui-radio-disabled").addClass("layui-disabled").attr('disabled', true);
            $('input[name="settle"]').addClass("layui-radio-disabled").addClass("layui-disabled").attr('disabled', true);
        }

        form.render(null);
    }


    function change(type) {
        if (type == 1) {
            $('.type1').show();
            $('.type2').hide();
            $('#baseTypeNew_id').show();
            $('#commissionNew').attr('lay-verify', 'required|number|range_0_12');
        } else {
            $('.type1').hide();
            $('.type2').show();
            $('#baseTypeNew_id').hide();
            $('#commissionNew').attr('lay-verify', 'required|number|range_0_30');
        }
    }

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;
        var url = "/channel/modifyCommission.action";
        data.channelId = $('#channelId').val();
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json",
            dataType: "json", data: JSON.stringify(data),
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if(resp.status == 200) {
                    // zmnPage.direct("/channel/commission.action?channelId=" + resp.id + "&addFlag=" + json.addFlag, 0);
                    zmnPage.reload(resp.message, 1000);
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });

    // 启用/停用（批量）
    table.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "batchSet") {
            extracted({servCategId:'',commissionTypeNew:1,baseTypeNew:1,commissionNew:'', effectiveTime:''});
        }
    });
    
    //对外暴露的接口
    exports('channel_commission', {});
});